package org.example.day20240902;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class urlMain {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int rule_count = scanner.nextInt();
        int url_count = scanner.nextInt();
        scanner.nextLine();
        Map<String,String> ruleMap = new LinkedHashMap<>();
        for(int i = 0;i < rule_count;i++){
//            /articles/<int>/<int>/<str>/ article_detail
            String line = scanner.nextLine();
            String[] arr = line.split(" ");
            String rule = arr[0];
            rule = rule.replace("<int>","([0-9]+)");
            rule = rule.replace("<str>","([^/]+)");
            rule = rule.replace("<path>","(.+)");
            ruleMap.put(arr[1], rule);
        }

        for(int i = 0;i<url_count;i++){
//            /articles/1985/09/aloha/
            String line = scanner.nextLine();
            boolean bFind = false;
            for(Map.Entry<String,String> entry : ruleMap.entrySet()){
                Pattern pattern = Pattern.compile(entry.getValue());
                Matcher matcher = pattern.matcher(line);
                if(matcher.matches()){
                    System.out.print(entry.getKey()+" ");
                    int groupCount = matcher.groupCount();
                    for(int j= 1;j<=groupCount;j++){
                        if(matcher.group(j).matches("[0-9]+")){
                            System.out.print(Integer.parseInt(matcher.group(j))+" ");
                        }
                        else{
                            System.out.print(matcher.group(j) + " ");
                        }

                    }
                    bFind = true;
                    break;
                }
            }
            if(!bFind){
                System.out.println("404");
            }
            else{
                System.out.println();
            }

        }
    }
}
